﻿@inject IActionService _actionService
@inject IDialogService _dialogService
@inject ChatState _chatState

<MudListItem Value="@Action!.Id">
    <ChildContent>
        <div class="d-flex flex-row flex-grow-1 gap-4">
            <MudIcon Icon="@Action.Icon" Size="@Size.Medium" Class="my-auto" />
            <MudText Typo="Typo.body1" Class="my-auto">@Action?.Name</MudText>
            <MudSpacer />
            <MudMenu Icon="@Icons.Material.Filled.MoreVert">
                <MudMenuItem OnClick="() => EditAction(Action!.Id)">Edit</MudMenuItem>
                <MudMenuItem OnClick="ConfirmDeleteActionDialog">Delete</MudMenuItem>
            </MudMenu>
        </div>
    </ChildContent>
</MudListItem>
<MudDivider />

@code 
{
    [Parameter] public NativeActionResponse? Action { get; set; }
    [Parameter] public EventCallback<Guid> OnEdit { get; set; }
    [Parameter] public EventCallback OnDelete { get; set; }

    private async Task EditAction(Guid id)
    {
        await OnEdit.InvokeAsync(id);
    }

    private async Task ConfirmDeleteActionDialog()
    {

        var parameters = new DialogParameters
        {
            { "Message", $"Confirm deletion of action '{Action?.Name}'?" },
            { "DetailMessage", "This action cannot be undone." }
        };

        var options = new DialogOptions { CloseOnEscapeKey = true };
        var dialog = _dialogService.Show<ConfirmDialog>("Delete Action", parameters, options);

        var result = await dialog.Result;

        if (!result.Canceled)
        {
            await _actionService.DeleteAction(Action!.Id);
            await OnDelete.InvokeAsync();
            _chatState.NotifyActionsRefreshed();
        }
    }
}
